<?php

namespace App\Http\Controllers\Shop;

use App\Http\Controllers\ShopController;
use App\Models\Shop\GoodsClassify;
use App\Models\Shop\Recommend;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\DB;

class ListController extends ShopController
{
    public function getIndex(Request $request)
    {
        $this->active = 'list';
        $typeID = $request->has('typeID') ? $request->input('typeID') : 0;
        $recommendID = $request->has('recommendID') ? $request->input('recommendID') : 0;
        $storeID = \Illuminate\Support\Facades\Request::cookie('storeID') ? \Illuminate\Support\Facades\Request::cookie('storeID') : 1;
        $page = ($request->has('page') ? $request->input('page') : 1) - 1;

        $goods = DB::table('shop_store_goods')
            ->join('shop_goods', 'shop_store_goods.goodsID', '=', 'shop_goods.id')
            ->where('shop_store_goods.storeID', $storeID)
            ->where('shop_store_goods.display', 1);
        if($typeID)
        {
            $goods = $goods->where('shop_goods.classifyID',$typeID);
        }
        if($recommendID)
        {
            //获取推荐位信息
            $recommend = Recommend::find($recommendID);
            if($recommend)
            {
                if($recommend->goods)
                {
                    $goodsID = explode(',', $recommend->goods);
                    $goods = $goods->whereIn('shop_store_goods.id',$goodsID);
                }else{
                    $goods = $goods->whereIn('shop_store_goods.id',[]);
                }
            }
        }
        $goods = $goods->select('shop_store_goods.id', 'shop_store_goods.hit', 'shop_store_goods.storeID', 'shop_store_goods.amount', 'shop_store_goods.realAmount', 'shop_store_goods.number', 'shop_store_goods.display',
            'shop_goods.classifyID', 'shop_goods.title', 'shop_goods.thumb', 'shop_goods.abstract')
            ->orderBy('id', 'desc')
            ->skip($page*10)
            ->take(10)
            ->get();
        //获取分类
        $classify = GoodsClassify::all()->keyBy('id');
        //获取推荐位
        $recommends = Recommend::where('storeID', $storeID)->where('display', 1)->get()->keyBy('id');
        return view('shopWx.list')->with(['recommends' => $recommends, 'active' => $this->active, 'goods' => $goods, 'classify' => $classify]);
    }

    public function getIndexAjax(Request $request)
    {
        $typeID = $request->has('typeID') ? $request->input('typeID') : 0;
        $recommendID = $request->has('recommendID') ? $request->input('recommendID') : 0;
        $storeID = \Illuminate\Support\Facades\Request::cookie('storeID') ? \Illuminate\Support\Facades\Request::cookie('storeID') : 1;
        $page = ($request->has('page') ? $request->input('page') : 1) - 1;

        $goods = DB::table('shop_store_goods')
            ->join('shop_goods', 'shop_store_goods.goodsID', '=', 'shop_goods.id')
            ->where('shop_store_goods.storeID', $storeID)
            ->where('shop_store_goods.display', 1);
        if($typeID)
        {
            $goods = $goods->where('shop_goods.classifyID',$typeID);
        }
        if($recommendID)
        {
            //获取推荐位信息
            $recommend = Recommend::find($recommendID);
            if($recommend)
            {
                if($recommend->goods)
                {
                    $goodsID = explode(',', $recommend->goods);
                    $goods = $goods->whereIn('shop_store_goods.id',$goodsID);
                }else{
                    $goods = $goods->whereIn('shop_store_goods.id',[]);
                }
            }
        }
        $goods = $goods->select('shop_store_goods.id', 'shop_store_goods.hit', 'shop_store_goods.storeID', 'shop_store_goods.amount', 'shop_store_goods.realAmount', 'shop_store_goods.number', 'shop_store_goods.display',
            'shop_goods.classifyID', 'shop_goods.title', 'shop_goods.thumb', 'shop_goods.abstract')
            ->orderBy('id', 'desc')
            ->skip($page*10)
            ->take(10)
            ->get();
        $ajaxCode = $goods ? 1 : 0;
        $ajaxMsg = $goods ? 'success' : 'error';
        $lazyImg = asset('style/shopWx/img/lazy.png');
        foreach ($goods as $k=>$v)
        {
            $goods[$k]->thumb = action('ImgController@getPicture', ['sourceName' => $v->thumb, 'size' => '110x90']);
            $goods[$k]->lazyImg = $lazyImg;
        }
        return json_encode([
            'code'      =>  $ajaxCode,
            'msg'       =>  $ajaxMsg,
            'data'      =>  [
                'goods'     =>  $goods
            ]
        ]);
    }
}
